﻿//Copyright 2011 - Jonathan Allen

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using Granite;
using Granite.Eventing;

namespace Granite.Collections
{
    ///// <summary>
    ///// A ReadOnlyObservableCollection that can be garbage collected. (Normal ReadOnlyObservableCollections cannot be collected as long as the underlying collection exists.)
    ///// </summary>
    ///// <typeparam name="T">The type of elements in the collection.</typeparam>
    ///// <remarks>Use ExtendedReadOnlyObservableCollection when wrapping an ExtendedObservableCollection if you need to listen to changes to properties on child objects.</remarks>
    //public class WeakReadOnlyObservableCollection<T> : ReadOnlyCollection<T>,
    //    INotifyCollectionChanged, INotifyPropertyChanged, INotifyCollectionChangedWeak, INotifyPropertyChangedWeak
    //{
    //    CollectionChangedEventManager m_CollectionChangeEventManager;
    //    PropertyChangedEventManager m_PropertyChangedEventManager;
    //    readonly CollectionChangedListener m_SourceCollectionChanged;
    //    readonly PropertyChangedListener m_SourcePropertyChanged;

    //    /// <summary>
    //    /// Initializes a new instance of the WeakReadOnlyObservableCollection
    //    /// class that serves as a wrapper around the specified System.Collections.ObjectModel.ObservableCollection.
    //    /// </summary>
    //    /// <param name="list">
    //    /// The System.Collections.ObjectModel.ObservableCollection with which to
    //    /// create this instance of the System.Collections.ObjectModel.ReadOnlyObservableCollection
    //    /// class.
    //    /// </param>
    //    public WeakReadOnlyObservableCollection(ObservableCollection<T> list)
    //        : base(list)
    //    {
    //        m_SourceCollectionChanged = new CollectionChangedListener(OnSourceCollectionChanged);
    //        m_SourcePropertyChanged = new PropertyChangedListener(OnSourcePropertyChanged);

    //        WeakEventHandler.AddHandler(list, m_SourceCollectionChanged);
    //        WeakEventHandler.AddHandler(list, m_SourcePropertyChanged);

    //        m_PreviousCount = Count;
    //    }


    //    int m_PreviousCount;

    //    void OnSourceCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    //    {
    //        OnCollectionChanged(e);
    //    }

    //    /// <summary>
    //    /// Occurs when an item is added or removed.
    //    /// </summary>
    //    public event NotifyCollectionChangedEventHandler CollectionChanged;

    //    /// <summary>
    //    /// Occurs when a property value changes.
    //    /// </summary>
    //    public event PropertyChangedEventHandler PropertyChanged;

    //    /// <summary>
    //    /// Raises the CollectionChanged
    //    /// event using the provided arguments.
    //    /// </summary>
    //    /// <param name="args">Arguments of the event being raised.</param>
    //    protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs args)
    //    {
    //        if (CollectionChanged != null)
    //            CollectionChanged(this, args);

    //        if (Count != m_PreviousCount)
    //        {
    //            m_PreviousCount = Count;
    //            OnPropertyChanged(CommonProperties.CountProperty);
    //        }
    //        OnPropertyChanged(CommonProperties.ItemIndexedProperty);
    //    }


    //    /// <summary>
    //    ///  Raises the PropertyChanged
    //    ///  event using the provided arguments.
    //    /// </summary>
    //    /// <param name="args">Arguments of the event being raised.</param>
    //    protected void OnPropertyChanged(PropertyChangedEventArgs args)
    //    {
    //        if (PropertyChanged != null)
    //            PropertyChanged(this, args);
    //    }



    //    /// <summary>
    //    /// Adds a weak event handler
    //    /// </summary>
    //    /// <param name="eventHandler"></param>
    //    public void AddHandler(ICollectionChangedListener eventHandler)
    //    {
    //        if (m_CollectionChangeEventManager == null)
    //            m_CollectionChangeEventManager = new CollectionChangedEventManager(this);

    //        m_CollectionChangeEventManager.AddHandler(eventHandler);
    //    }

    //    /// <summary>
    //    /// Removes a weak event handler
    //    /// </summary>
    //    /// <param name="eventHandler"></param>
    //    public void RemoveHandler(ICollectionChangedListener eventHandler)
    //    {
    //        if (m_CollectionChangeEventManager == null)
    //            return;

    //        m_CollectionChangeEventManager.RemoveHandler(eventHandler);
    //    }


    //    /// <summary>
    //    /// Adds a weak event handler
    //    /// </summary>
    //    /// <param name="eventHandler"></param>
    //    public void AddHandler(IPropertyChangedListener eventHandler)
    //    {
    //        if (m_PropertyChangedEventManager == null)
    //            m_PropertyChangedEventManager = new PropertyChangedEventManager(this);

    //        m_PropertyChangedEventManager.AddHandler(eventHandler);
    //    }

    //    /// <summary>
    //    /// Removes a weak event handler
    //    /// </summary>
    //    /// <param name="eventHandler"></param>
    //    public void RemoveHandler(IPropertyChangedListener eventHandler)
    //    {
    //        if (m_PropertyChangedEventManager == null)
    //            return;

    //        m_PropertyChangedEventManager.RemoveHandler(eventHandler);
    //    }

    //    /// <summary>
    //    /// This event is raised when a property on the source collection changes. You may use this to forward property change notifications for properties on this class that wrap the source collection.
    //    /// </summary>
    //    /// <remarks>
    //    /// Do not forward the properties "Count" and "Item[]". Only forward custom properties.
    //    /// </remarks>
    //    protected event RelayedEventHandler<PropertyChangedEventArgs> SourcePropertyChanged;

    //    void OnSourcePropertyChanged(object sender, PropertyChangedEventArgs e)
    //    {
    //        if (SourcePropertyChanged != null)
    //            SourcePropertyChanged(this, RelayedEvent.Create(sender, e));
    //    }

    //}
}
